/*
 * ip.cc
 *
 *  Created on: Dec 16, 2014
 *      Author: root
 */
#include <netinet/in.h>		// IPPROTO_TCP IPPROTO_UDP
#include <arpa/inet.h>		// ntohs
#include <stdio.h>
#include "ip.h"

bool Ip::IsTcp() {
	return this->protocol == IPPROTO_TCP;
}
bool Ip::IsUdp() {
	return this->protocol == IPPROTO_UDP;
}
bool Ip::IsOthers() {
	return !IsTcp() && !IsUdp();
}
char* Ip::ToString() {
#if 1
	static char info[1024] = { 0 };
	sprintf(info, "IPv%d, %d, %d, %d", this->version, this->ihl * 4,
			GetIpPayloadLen(), ntohs(this->tot_len));
	printf("%s\n", info);
	return info;
#endif
}
unsigned int Ip::GetIpPayloadLen() {
	return ntohs(this->tot_len) - this->ihl * 4;
}
unsigned char* Ip::GetIpPayload() {
	return (unsigned char*) this + this->ihl * 4;
}
char* Ip::GetAddrInDot(unsigned int addr) {
	return inet_ntoa(*((struct in_addr*) &addr));
}
